
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package csse3004.GD.SysAdmin;

import csse3004.GD.DA.SysAdminDA;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 *
 * @author s4201567 Tony Yu
 */
public class EditPADetailsController extends SimpleFormController {
    
    private SysAdminService sds = new SysAdminDA();
    private EditPADetails epd = new EditPADetails();
    protected final Log logger = LogFactory.getLog(getClass());

    public ModelAndView onSubmit(Object command) throws ServletException {   
        logger.info("hello");
        
        EditPADetails ep = (EditPADetails)command;
        
        int pid = epd.getAdminID();
        int type = epd.getType();
        
        String expiry = ep.getNumExpiryDays();
        String email = ep.getEmail();
        
        logger.info("Pid: " + epd.getpAdminID());
        logger.info("type: " + type);
        
        if (type == 0) {        //expiry
            logger.info("Expiry: " + expiry);
            sds.editUserExpiry(pid, expiry);
        } else if (type == 1) { //email
            logger.info("pid: " + pid);
            logger.info("Email: " + ep.getEmail());
            sds.editUserEmail(pid, email);
        }
        
        String suc = "The field has been successfully updated!";
        
        ModelAndView mnv = new ModelAndView("successMsg", "suc", suc);  
        return mnv;
    }
    
    @Override
    protected Object formBackingObject(HttpServletRequest hsr) throws ServletException {         
        try {
        epd.setType(Integer.parseInt(hsr.getParameter("type")));
        epd.setAdminID(Integer.parseInt(hsr.getParameter("pid")));
        return epd;
        }catch (Exception e){
            logger.info(e);
            return epd;
        }
    }
}
    
    /*
     * TODO CAPTAIN THOMAS.L.MCNAIR BSC, SSC
     * 
     * Function which retrieves Poll Administrator account details. Their email address and number of days until expiry.
     * 
     * 
     * 
     * 
     *     @Override
    protected Object formBackingObject(HttpServletRequest hsr) throws ServletException {         

        epd.setpAdminID((String)hsr.getParameter("pid"));
        epd.setType(Integer.parseInt(hsr.getParameter("type")));
        return epd;
    }

     * */
/*    
 * 
 * 
 * 
 *         String strPid = hsr.getParameter("pid");
        int pid = 0;
        
        if (strPid != null) {
            pid = Integer.parseInt(strPid);
            logger.info("hello1");
        }
 * 
 * 
 * 
        ModelAndView mnv = new ModelAndView(new RedirectView(getSuccessView()));
        
        if (hsr.getParameter("pid") != null) {
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            mnv.addObject("pid", pid);
        }
 * 
 * 
public class EditPADetailsController implements Controller {
    
    @RequestMapping(method = RequestMethod.GET)    
    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        
        if (hsr.getParameter("pid") != null) {
            int pid = Integer.parseInt(hsr.getParameter("pid"));
            
            ModelAndView mnv = new ModelAndView("editPADetails");
            mnv.addObject("pid", pid);
            return mnv;
        }
        return new ModelAndView("editPADetails");
    }
}

 */